<template>
  <div class='login_container'>
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/avatar.jpg" alt="">
      </div>
      <el-form :model="loginForm" ref="LoginFormRef" :rules="loginFormRules" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"
            placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import user from '@/api/user'

export default {
  name: 'MyLogin',
  data () {
    return {
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '登录名长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      },
      loginForm: {
        username: '',
        password: ''
      }
    }
  },
  components: {
  },
  methods: {
    // 1.登录事件
    login () {
      // 兜底校验
      this.$refs.LoginFormRef.validate(async valid => {
        if (valid) {
          const { data: res } = await user.login(this.loginForm)
          if (res.meta.status === 200) {
            const token = res.data.token
            // 存取token到sessionStorage中
            sessionStorage.setItem('token', token)
            // 跳转主页
            this.$router.push('/')
            this.$message.success(res.meta.msg)
          } else {
            this.$message.error(res.meta.msg)
          }
        } else {
          return false
        }
      })
    },
    // 2.重置表单
    resetLoginForm () {
      this.$refs.LoginFormRef.resetFields()
    }
  },
  created () {
  },
  mounted () {
  }
}
</script>

<style lang='less' scoped>
.login_container {
  background-color: skyblue;
  height: 100%;
}

.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;

  .avatar_box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    height: 130px;
    width: 130px;
    border-radius: 50%;
    border: 1px solid #ccc;
    padding: 10px;
    box-shadow: 0 0 10px #ccc;
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }

  .login_form {
    box-sizing: border-box;
    position: absolute;
    bottom: 0;
    padding: 20px;
    width: 100%;
    text-align: center;
  }
}
</style>
